<template>
  <el-dialog :title="title" class="addPedestal" width="380px" :visible.sync="isDial">
    <el-form :model="form" size="mini" :rules="rules" ref="form">
      <el-form-item label="栋座楼层" :label-width="formLabelWidth">
        <el-input
          size="mini"
          :disabled="true"
          v-model="form.pedestallayer"
          autocomplete="off"
          style="width:182px"
        ></el-input>
      </el-form-item>
      <el-form-item label="房号" prop="roomNum" :label-width="formLabelWidth">
        <el-input
          size="mini"
          v-model="form.roomNum"
          class="leftBorder"
          :maxlength="10"
          autocomplete="off"
          style="width:182px"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="saveRoom">保 存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { request } from "../../../assets/request.js";
export default {
  data() {
    return {
      form: {
        roomNum: "", //房号
        pedestallayer: "" //栋座楼层
      },
      formLabelWidth: "80px",
      isDial: false,
      title: "",
      info: "",
      rules: {
        roomNum: [
          {
            pattern: /^[\s\S]*.*[^\s][\s\S]*$/,
            required: true,
            message: "房号是必填项",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    let that = this;
    this.$root.$on("addRoom", (type, data, tit) => {
      that.isDial = true;
      that.info = data;
      let form = that.form;
      if (type == "edit") {
        that.title = "编辑房号";
        form.roomNum=data.no
      } else {
        that.title = "添加房号";
        that.resetForm('form')
      }
      form.pedestallayer = tit;
      that.form = form;
    });
  },
  methods: {
    saveRoom() {
      let that = this;
      this.$refs.form.validate(valid => {
        console.log(this.info);
        let obj = {
          housesId: this.info.housesId,
          housesUnitId: this.info.housesUnitId,
          no: this.form.roomNum,
          tier: parseInt(this.info.tier.substr(0, this.info.tier.length - 1))
        };
        console.log(obj);
        let url = "houses/number";
        let met = "POST";
        if (that.title == "编辑房号") {
          met = "PUT";
          // obj.id=that.id
          url =
            "houses/number?number=" + that.form.roomNum + "&id=" + that.info.id;
          request.ajaxPost({
            url,
            that,
            type: met,
            fn(res) {
              console.log(res);
              that.isDial = false;
              // that.resetForm("ruleForm");
              that.$root.$emit("seeRoom")
            }
          });
        } else {
          request.ajaxPost({
            url,
            that,
            params: obj,
            fn(res) {
              console.log(res);
              that.isDial = false;
              // that.resetForm("ruleForm");
              that.$root.$emit("seeRoom")
            }
          });
        }
      });
    },
    resetForm(formName) {
      if (this.$refs[formName]!==undefined) {
          this.$refs[formName].resetFields();
      }
    },
  }
};
</script>
<style lang="less" scoped>
</style>


